# -*- coding: utf-8 -*-
'''
Created on 14.03.2011

@author: mutius
'''
import wx.grid
from GenericTable import GenericTable
from GlobalVars import global_settings

class MyGrid(wx.grid.Grid):
    table = []
    def __init__(self, parent, dc, table, rowLabels=None, colLabels=None):
        wx.grid.Grid.__init__(self, parent, -1)
        #self.SetTable(GenericTable(table, rowLabels, colLabels))
        self.dc = dc
        self.setData(table, rowLabels, colLabels)
    def setData(self, data, rowLabels=None, colLabels=None):
        if len(data) == 0 and colLabels != None:
            data = [[]]
            data[0] = [" "]*len(colLabels)
        self.SetTable(GenericTable(data, rowLabels, colLabels))
        if len(data) > 0 and colLabels != None and len(data[0]) > 0 and global_settings["gridFormat"]:
            sizes = len(data[0])*[0.1]
            minSize = self.dc.GetTextExtent('W')[0]
            for i in xrange(len(data)):
                for j in xrange(len(data[0])):
                    value = self.GetCellValue(i, j)
                    size = self.dc.GetTextExtent(value)[0]
                    #size = max(map(lambda x:len(x),value.split('\n')))*minSize
                    if size > sizes[j]:
                        sizes[j] = size;
            for i in xrange(len(data[0])):
                size = self.dc.GetTextExtent(colLabels[i])[0]
                if size > sizes[i]:
                    sizes[i] = size
                self.SetColSize(i, sizes[i] + 10)
                
            sizes = len(data)*[0.1]
            minSize = self.dc.GetTextExtent('Q')[1]
            for i in xrange(len(data[0])):
                for j in xrange(len(data)):
                    value = self.GetCellValue(j, i)
                    #size = self.dc.GetTextExtent(value)[1]
                    size = len(value.split('\n'))*minSize
                    if size > sizes[j]:
                        sizes[j] = size;
            for i in xrange(len(data)):
                self.SetRowSize(i, sizes[i] + 10)
                pass